package com.example.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class ReviewAdapter extends RecyclerView.Adapter<ReviewAdapter.ReviewViewHolder> {

    private final List<Review> reviewList;
    private final Context context;
    private final DBHelper dbHelper;

    public ReviewAdapter(List<Review> reviewList, Context context) {
        this.reviewList = reviewList;
        this.context = context;

        dbHelper = new DBHelper(context);
    }

    @NonNull
    @Override
    public ReviewViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_review, parent, false);
        return new ReviewViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ReviewViewHolder holder, int position) {
        Review review = reviewList.get(position);

        holder.tvProductName.setText("商品: " + review.getProductName());
        holder.tvTransactionId.setText("交易ID: " + review.getTransactionId());
        holder.tvComment.setText("评论: " + review.getComment());
        holder.ratingBar.setRating(review.getRating());

        // 点击事件 - 修改评价
        holder.itemView.setOnClickListener(v -> showEditReviewDialog(review));
    }

    // 弹出修改评价对话框
    private void showEditReviewDialog(Review review) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("修改评价");

        View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_edit_review, null, false);
        EditText etComment = dialogView.findViewById(R.id.etComment);
        RatingBar rbRating = dialogView.findViewById(R.id.rbRating);

        // 初始化对话框内容
        etComment.setText(review.getComment());
        rbRating.setRating(review.getRating());

        builder.setView(dialogView);

        builder.setPositiveButton("保存", (dialog, which) -> {
            String newComment = etComment.getText().toString().trim();
            int newRating = (int) rbRating.getRating();

            if (newComment.isEmpty()) {
                Toast.makeText(context, "评价内容不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            // 更新数据库
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("comment", newComment);
            values.put("rating", newRating);

            int rowsAffected = db.update("reviews", values, "review_id = ?", new String[]{String.valueOf(review.getReviewId())});
            db.close();

            if (rowsAffected > 0) {
                // 更新列表中的数据
                review.setComment(newComment);
                review.setRating(newRating);
                notifyDataSetChanged();
                Toast.makeText(context, "评价已更新", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "更新失败，请重试", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("取消", (dialog, which) -> dialog.dismiss());
        builder.show();
    }

    @Override
    public int getItemCount() {
        return reviewList.size();
    }

    public static class ReviewViewHolder extends RecyclerView.ViewHolder {
        TextView tvProductName, tvTransactionId, tvComment;
        RatingBar ratingBar;

        public ReviewViewHolder(@NonNull View itemView) {
            super(itemView);
            tvProductName = itemView.findViewById(R.id.tvProductName);
            tvTransactionId = itemView.findViewById(R.id.tvTransactionId);
            tvComment = itemView.findViewById(R.id.tvComment);
            ratingBar = itemView.findViewById(R.id.ratingBar);
        }
    }
}
